package com.rk.kbk.common;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * Created by chengwenwen on 2016/12/20.
 */
public class OrderUtils {
    private static long orderNum = 0l;
    private static String date ;


    /**
     * 生成订单编号(配送订单)
     * @return
     */
    public static synchronized String getOrderNo() {
        String str = new SimpleDateFormat("yyyyMMdd").format(new Date());
        String base = "0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < 9; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        String orderNumber=str+sb.toString();
        return orderNumber;
    }

    /**
     * 生成订单编号(商城订单)
     * @return
     */
    public static synchronized String getGoodsOrderNo() {
        String str = new SimpleDateFormat("yyyyMMdd").format(new Date());
        String base = "0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < 7; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        String orderNumber=str+sb.toString();
        return orderNumber;
    }


    /**
     * 余额支付交易订单编号
     * @return
     */
    public static synchronized String getBalancePayNo() {
        String str = new SimpleDateFormat("yyyyMMddHHmm").format(new Date());
        if(date==null||!date.equals(str)){
            date = str;
            orderNum  = 0l;
        }
        orderNum ++;
        long orderNo = Long.parseLong((date)) * 10000;
        orderNo += orderNum;
        return "xx4009772001"+orderNo+"";
    }
}
